import { defineStore } from 'pinia'
import { loginApi } from '@/api/user/index'

/* const useCommonStore = defineStore('common', () => {
  const user = ref({})

  const login = async (data) => {
    const res = await loginApi(data)
    if (res.code == 200) {
      user.value = res.data
      localStorage.setItem('common', user.value)
    }
    return res
  }

  return { user, login }
})*/
const useUserStore = defineStore('user', {
  state: () => {
    return {
      user: {},
      token: null,
      isCollapse: false,
      defaultActive: '/',
    }
  },
  actions: {
    // 登录
    async login(data) {
      const res = await loginApi(data)
      if (res.code == 200) {
        this.user = res.data.user
        this.token = res.data.token
      }
      return res
    },
    // 切换菜单面板
    changeCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 退出登录
    loginOut() {
      this.user = {}
      this.token = ''
      this.isCollapse = true
    },
    // 修改激活下
    changeActive(index) {
      this.defaultActive = index
    },
  },
  persist: true,
})

export default useUserStore
